package com.spremoteservice.android;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	//public final static String EXTRA_MESSAGE = "com.spremoteservice.android.MESSAGE";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
    	EditText txtSiteUrl = (EditText) findViewById(R.id.site_url);
    	EditText txtUserName = (EditText) findViewById(R.id.user_name);
    	EditText txtPassword = (EditText) findViewById(R.id.password);

    	SharedPreferences settings = getSharedPreferences("UserInfo", 0);
		txtSiteUrl.setText(settings.getString("SiteUrl", "").toString());
		txtUserName.setText(settings.getString("UserName", "").toString());
		txtPassword.setText(settings.getString("Password", "").toString());
	}
		
    /** Called when the user clicks the Send button 
     * @throws TimeoutException 
     * @throws ExecutionException 
     * @throws InterruptedException */
    public void login(View view) throws InterruptedException, ExecutionException, TimeoutException {
        // Do something in response to button
    	Intent intent = new Intent(this, SiteContents.class);
    	EditText txtSiteUrl = (EditText) findViewById(R.id.site_url);
    	EditText txtUserName = (EditText) findViewById(R.id.user_name);
    	EditText txtPassword = (EditText) findViewById(R.id.password);
    	Boolean isValid = true;
    	if (isValid) {
    		SharedPreferences settings = getSharedPreferences("UserInfo", 0);
    		SharedPreferences.Editor editor = settings.edit();
    		editor.putString("SiteUrl",txtSiteUrl.getText().toString());
    		editor.putString("UserName",txtUserName.getText().toString());
    		editor.putString("Password",txtPassword.getText().toString());
    		editor.commit();
    		
    		CallAsyncSP task = new CallAsyncSP();
    		try{
        		task.execute(new String[]{txtSiteUrl.getText().toString(),txtUserName.getText().toString(),txtPassword.getText().toString()});
        		task.get(120000, TimeUnit.MILLISECONDS);
        		ListViewSPObject web = task.GetResult();
        		if(web == null){
        			Utils.ShowDialog(this, "Cannot login", "Error");
        			return;
        		}
        		intent.putExtra("ListViewSPObjectParent", web);
        		startActivity(intent);
    		}catch (Exception e){
    			e.printStackTrace();
    			Utils.ShowDialog(this, "Cannot login", "Error");
    		}
    	} else {
    		Utils.ShowDialog(this, "Please enter a valid Site Url, User name and Password", "Error");
    	}
    }
    

}

class CallAsyncSP extends AsyncTask<String, Void, ListViewSPObject>{
	
	private ListViewSPObject result;
	
	public ListViewSPObject GetResult(){
		return result;
	}
	
	@Override
	protected ListViewSPObject doInBackground(String... arg0) {
		String siteUrl = arg0[0];
		String userName = arg0[1];
		String password = arg0[2];
		ListViewSPObject web = null;
		try {
			web = new Connector(siteUrl, userName, password).GetWeb(siteUrl);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		result = web;
		return web;
	}
	
}